<script setup>
// 传送带管理
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getBeltPage } from '@/api/belt'
onMounted(() => {
  search()
})
const pageNo = ref(1);
const pageSize = ref(10);
const background = ref(true)
const total = ref(0)
const handleSizeChange = (val) => {
  // 添加边界校验
  if (val < 1) val = 1;
  pageSize.value = val;
  pageNo.value = 1; // 重置到第一页
  search();
};

const handleCurrentChange = (val) => {
  // 添加边界校验
  if (val < 1) val = 1;
  if (val > Math.ceil(total.value / pageSize.value)) {
    val = Math.ceil(total.value / pageSize.value);
  }
  pageNo.value = val;
  search();
};
// 动态加载数据 - 查询部门
const search = async () => {
  const result = await getBeltPage(pageNo.value, pageSize.value)
  beltList.value = result.data.data.records
}

// 钩子函数
onMounted(() => {
 search()
})
const belt = ref({
  id: '',
  name: ''
})
const beltList = ref([])

const addBelt = () => {
  dialogVisible.value = true;
  dialogTitle.value = '新增传送带'
}
const dialogVisible = ref(false)
const dialogTitle = ref('')
const beltFormRef = ref()
</script>

<template>
  <h1>传送带管理</h1>
  <br>

  <el-button type="primary" @click="addBelt" style="float: left;"> + 新增传送带</el-button> <br><br>

  <!-- 数据展示表格 -->
  <el-table :data="beltList" border style="width: 100%;">
    <el-table-column prop="id" label="id" width="300" align="center"/>
    <el-table-column prop="beltName" label="传送带名称" width="300" align="center"/>
    <el-table-column label="操作" fixed="right" align="center">
      <template v-slot="scope">
        <el-button type="primary" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
        <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <el-dialog v-model="dialogVisible" :title="dialogTitle"  >
    <el-form :model="belt" label-width="80px" :rules="rules" ref="beltFormRef"  >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="id" prop="id">
            <el-input v-model="belt.id" placeholder="请输入传送带id" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="名称" prop="name">
            <el-input v-model="belt.name" placeholder="请输入传送带名称" />
          </el-form-item>
        </el-col>
      </el-row>

      
    <el-form-item>
      <el-button type="success" @click="save()">确认</el-button>
      <el-button type="primary" @click="dialogVisible = false">取消</el-button>
    </el-form-item>
  </el-form>
  </el-dialog>
    <br>
    <br>
    <div class="container">
      <el-pagination
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :page-sizes="[5, 10, 20, 50]"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    </div>

</template>

<style scoped>

</style>
